package test;

import java.io.IOException;

import error.BuildingAlreadyExitsException;
import error.NotEnoughWaresException;

import buildings.*;

import game.City;

public class ConsoleGame {

	private City city = null;
	
	void startGame() {
		boolean gameMenu = true;
		boolean buildMenu;
		city = new City("");
		
		String input;
		do{
			System.out.println("CITY:\n" + city.waresToString() +"\nMENU - Please choose:\n >build\n >upgrade\n >view city\n >next round\n >rounds+RUNDENZAHL\n >exit");
        	input = read();
			if (input.equals("build")) {
				buildMenu=true;
				do{
					System.out.println("CITY:\n" + city.waresToString() + "\nBUILD - Please choose:\n >farm\n >lumberjack\n >quarry\n >ore mine\n\n >back");
					input = read();
					if (input.equals("farm")) {
						build(new Farm());
					} else if (input.equals("lumberjack")) {
						build(new Lumberjack());
					} else if (input.equals("quarry")) {
						build(new Quarry());
					} else if (input.equals("ore mine")) {
						build(new Ore_Mine());
					}else if (input.equals("back")) {
						buildMenu=false;
					}
				}while(buildMenu);
			} else if (input.equals("view city")) {
				for (int i=0; i<city.getBuildings().size(); i++) {
					AbstractBuilding x = city.getBuildings().get(i);
					System.out.println(" " + x.getClass().getSimpleName() + " - Level " + x.getLevel() );
				}
				System.out.println("Enter for back." );
				read();
			}  else if (input.equals("upgrade")) {
				System.out.println("\nSORRY! Not implemented yet.");
			} else if (input.equals("next round")) {
				System.out.println("\nNext Round...");
				city.round();
			} else if (input.startsWith("rounds+")) {
				Integer r = Integer.valueOf((String)input.subSequence(7,input.length()));
				for (int i = 0; i<r; i++) {
					city.round();
				}
				
			} else if (input.equals("exit")) {
				System.out.println("\nThank you for playing!");
				gameMenu=false;
			}
			
		}while(gameMenu);
	}
	
	String read(){
	    byte buffer[] = new byte[80];  // Zeichenpuffer
	    String input = "";
	    int read;
		try {
			read = System.in.read(buffer, 0, 80);
			input = new String(buffer, 0, read);
			input=input.substring(0, input.length()-2);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return input.toLowerCase().trim();

	}
	
	void build(AbstractBuilding a) {
		System.out.println("BUILD:\n" +  a.getClass().getSimpleName()+  ".\n\n" + a.getCosts().toString(1)+"\n\nBuild?\n >Yes\n >No");
		String input = read();
		if (input.equals("yes")) {
			//TODO: Testen ob genug Res vorhanden sind!!
			try {
				city.buildOrUpgradeBuilding(a);
				System.out.println("You build a " + a.getClass().getSimpleName());
			} catch (BuildingAlreadyExitsException e) {
				System.out.println("You already built a " + a.getClass().getSimpleName() + ". Upgrade instead.");
			} catch (NotEnoughWaresException e) {
				System.out.println("Not enough wares to built a " + a.getClass().getSimpleName() + ".");
			}
		}
	}
	
	
}
